//
//  Player.m
//  gong
//
//  Created by echo on 14-1-11.
//  Copyright (c) 2014年 echo. All rights reserved.
//

#import "Player.h"
#import "UICreator.h"
#import "PositionTools.h"
#import "AutoRemoveMessageView.h"
#import "Constants.h"

@implementation Player
{
    NSString *_imageName;
//    UIImageView *_imageView;
    __weak UIView *_containerView;
    NSArray *_randomScriptArray;
    NSInteger _currentScriptIndex;
}

SharedInstanceBuilder(Player);

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id)init
{
    self = [super init];
    if (self) {
        self.rank = 1;
        self.name = @"小燕子";
        _randomScriptArray = @[@"今天天气不错。",@"我该如何回到我的时代啊。",@"鹅鹅鹅，曲项向天歌。",@"我是一个快乐的小宫女。"];
    }
    return self;
}

- (void)init:(UIView *)container
{
    _containerView = container;
    
    _imageName = @"player.png";
    _imageView = [UICreator createImageViewFromImagename:_imageName use2X:NO];
    [PositionTools placeView:_imageView atTheLeftBottomOfTheView:_containerView offset:CGSizeMake(60, -80)];
    
    _imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)];
    [_imageView addGestureRecognizer:tap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whenEnterPlace) name:MsgEnterPlace object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whenLeavePlace) name:MsgLeavePlace object:nil];
}

- (void)whenEnterPlace
{
    _imageView.alpha = 0;
}

- (void)whenLeavePlace
{
    _imageView.alpha = 1;
}

- (void)handleTap
{
    if (_currentScriptIndex < _randomScriptArray.count) {
        NSString *script = _randomScriptArray[_currentScriptIndex];
        [AutoRemoveMessageView show:script
                  withContainerView:_containerView
                         completion:nil];
        
        _currentScriptIndex++;
        if (_currentScriptIndex > _randomScriptArray.count - 1) {
            _currentScriptIndex = 0;
        }
    }
//    [AutoRemoveMessageView show:[NSString stringWithFormat:@"我是%@", self.name]
//              withContainerView:_containerView
//                     completion:nil];
}
@end
